#!/bin/bash

. /opt/module/rebate-report/configration/config.sh

result_url=${BI_URL}
result_database=${BI_DATABASE}
result_username=${BI_USERNAME}
result_password=${BI_PASSWORD}

if [ -n "$2" ] ;then
        echo "如果是输入的日期按照取输入日期"
        do_date=$2
else
        echo "====没有输入数据的日期，取当前时间的前一天===="
        do_date=`date -d yesterday +"%Y-%m-%d"`
fi
echo "日期为：$do_date"

#清空表
del_dim_partition="TRUNCATE TABLE  app_rebate_partition;"
del_dim_cart="TRUNCATE TABLE  app_dim_cart;"
del_dim_company="TRUNCATE TABLE  app_dim_company;"
del_dim_station="TRUNCATE TABLE  app_dim_station;"
del_dim_station_carts="TRUNCATE TABLE  app_dim_station_carts;"

#每天无分区全量
export_tb(){
sqoop export -D mapred.job.queue.name=root.tianyan \
--connect "jdbc:mysql://$result_url/$result_database?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false" \
--username  $result_username \
--password  $result_password \
--table $1 \
-m $2 \
--export-dir hdfs://nameservice:8020/warehouse/dw/$3 \
--fields-terminated-by '\001' \
--input-null-string '\\N' \
--input-null-non-string '\\N' \
--columns "$4"
}

#每天有分区全量
export_tb_p(){
sqoop export -D mapred.job.queue.name=root.tianyan \
--connect "jdbc:mysql://$result_url/$result_database?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false" \
--username  $result_username \
--password  $result_password \
--table $1 \
-m $2 \
--hcatalog-database $3 \
--hcatalog-table $4 \
--hcatalog-partition-keys dt \
--hcatalog-partition-values $do_date \
--fields-terminated-by '\001' \
--input-null-string '\\N' \
--input-null-non-string '\\N' \
--columns "$5"
}


export_dim_partition(){
mysql -h${result_url}  -P3306  -u${result_username} -p${result_password} ${result_database} -e  "${del_dim_partition}"
export_tb_p "app_rebate_partition" "1" "dw" "dim_partition" "
business_type,
partition_dt
"
}

export_dim_profit_cart(){
mysql -h${result_url}  -P3306  -u${result_username} -p${result_password} ${result_database} -e  "${del_dim_cart}"
export_tb "app_dim_cart" "1" "dwd/dim_profit_cart" "si_id, p_id, p_number, p_user_id, p_user_name, p_brand_id, p_brand_name, p_name, cart_id, cart_number, cart_user_id, cart_user_name, cart_brand_id, cart_brand_name, cart_name"
}

export_dim_profit_company(){
mysql -h${result_url}  -P3306  -u${result_username} -p${result_password} ${result_database} -e  "${del_dim_company}"
export_tb "app_dim_company" "1" "dwd/dim_profit_company" "group_id,customer_number,p_group_name,group_name,saler_id,saler_name,province_id,province_name,team_id,team_name,is_special_fund"
}

export_dim_profit_station(){
mysql -h${result_url}  -P3306  -u${result_username} -p${result_password} ${result_database} -e  "${del_dim_station}"
export_tb "app_dim_station" "1" "dwd/dim_profit_station" "station_id, station_number, station_name, station_status, station_brand_id, station_brand_name, region_id, region_name, web_type, road_type, use_time"
}

export_dim_station_carts(){
mysql -h${result_url}  -P3306  -u${result_username} -p${result_password} ${result_database} -e  "${del_dim_station_carts}"
export_tb "app_dim_station_carts" "1" "dwd/dim_station_carts" "si_id, p_ids, p_numbers, p_user_ids, p_user_names, p_brand_ids, p_brand_names, p_names, cart_ids, cart_numbers, cart_user_ids, cart_user_names, cart_brand_ids, cart_brand_names, cart_names"
}

case $1 in
"dim_partition")
export_dim_partition
;;
"dim_profit_cart")
export_dim_profit_cart
;;
"dim_profit_company")
export_dim_profit_company
;;
"dim_profit_station")
export_dim_profit_station
;;
"dim_station_carts")
export_dim_station_carts
;;
"all")
export_dim_partition
export_dim_profit_cart
export_dim_profit_company
export_dim_profit_station
export_dim_station_carts
;;
esac